package com.cuitrip.business.tripservice;

import android.content.Context;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.cuitrip.business.tripservice.event.ServiceChangeEvent;
import com.cuitrip.business.tripservice.model.FormType;
import com.cuitrip.business.tripservice.model.ServiceInfo;
import com.cuitrip.service.R;
import com.cuitrip.util.SpanUtil;
import com.lab.app.customui.CustomUiConfig;
import com.lab.app.customui.CustomUiFragment;
import com.lab.jumper.FragmentJumper;
import com.lab.rx.bus.RxBus;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by sheena on 16/4/18.
 */
public class CreateTripTitleFragment extends CustomUiFragment {
    public static final String MODIFY_FLAG = "modify_flag";
    @Bind(R.id.et_trip_title)
    EditText etTripTitle;
    @Bind(R.id.tv_text_length)
    TextView tvTextLength;

    @Bind(R.id.desc)
    TextView desc;
//    @Bind(R.id.tv_ep)
//    TextView tvEP;
    @Bind(R.id.tv_btn_next)
    TextView tvBtn;
    //设置输入最大字数
    private int maxLength=80;
    private ServiceInfo serviceInfo;
    private String title;
    private boolean isModify;
    public static CreateTripTitleFragment newInstance(ServiceInfo serviceInfo,boolean isModify) {
        CreateTripTitleFragment createTripTitleFragment = new CreateTripTitleFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(ServiceInfo.KEY,serviceInfo);
        bundle.putBoolean(MODIFY_FLAG,isModify);
        createTripTitleFragment.setArguments(bundle);
        return createTripTitleFragment;
    }

    @Override
    public boolean initCloseTopBar() {
        return false;
    }

    @Override
    public CustomUiConfig initCustomUiConfig() {
        CustomUiConfig config = new CustomUiConfig();
        config.mLeftStyle= CustomUiConfig.AreaStyle.ICON_STYLE;
        config.mTopbarLeftAreaName=getString(R.string.back_icon);
        config.mTopbarMiddleAreaName=getString(R.string.trip_title);
        return config;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState, R.layout.activity_trip_title);
        if (getArguments()!=null){
            serviceInfo = (ServiceInfo) getArguments().getSerializable(ServiceInfo.KEY);
            title=serviceInfo.getName();
            isModify = getArguments().getBoolean(MODIFY_FLAG);
        }
        return view;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this, getView());
        etTripTitle.getBackground().mutate().setColorFilter(getResources().getColor(R.color.ganshi_ded8d7), PorterDuff.Mode.SRC_ATOP);
        if (!TextUtils.isEmpty(title)){
            etTripTitle.setText(title);
            tvBtn.setEnabled(true);
            tvTextLength.setText(title.length()+"/"+maxLength);
        }
        if (isModify){
            tvBtn.setText(getString(R.string.operation_ok_1));
        }
    }

    @Override
    public void initListener() {

        etTripTitle.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                tvTextLength.setText(s.length()+"/"+maxLength);
                if (!TextUtils.isEmpty(etTripTitle.getText())){
                    tvBtn.setEnabled(true);
                }
            }
        });
        SpannableStringBuilder epJump;
        if (serviceInfo.getCateList()==null || serviceInfo.getCateList().size()==0){
            epJump = SpanUtil.getEPJump(getString(R.string.trip_title_desc_2), getString(R.string.trip_content_intro_see_example), getHostActivity(), SpanUtil.TITLE_TYPE, "");
        }else {
            epJump = SpanUtil.getEPJump(getString(R.string.trip_title_desc_2), getString(R.string.trip_content_intro_see_example), getHostActivity(), SpanUtil.TITLE_TYPE, serviceInfo.getCateList().get(0).getCategory());
        }
        desc.setText(epJump);
        desc.setClickable(true);
        desc.setMovementMethod(new LinkMovementMethod() {
            @Override
            public void onTakeFocus(TextView view, Spannable text, int dir) {
                super.onTakeFocus(view, text, dir);
            }
        });
    }

    @OnClick(R.id.tv_btn_next)
    public void navigateCreateSuc() {
        if (isModify) {
            onBackPressed(false);
        } else {
            bindView2Model();
            InputMethodManager ma = (InputMethodManager) getHostActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            ma.hideSoftInputFromWindow(getHostActivity().getCurrentFocus().getApplicationWindowToken(), 0);
            FragmentJumper.addFragment(getHostActivity().getCustomSelfFragmentManager(), R.id.container, CreateTripSucFragment.newInstance(serviceInfo), CreateTripSucFragment.class.getName(), true);
        }
    }

    private void bindView2Model() {
        String title = etTripTitle.getText().toString();
        serviceInfo.setName(title);
    }

    private void saveAction(){
        bindView2Model();
        ServiceChangeEvent serviceChangeEvent = new ServiceChangeEvent(FormType.TITLE);
        RxBus.getDefault().send(serviceChangeEvent);
    }

    @Override
    public boolean onBackPressed(boolean fromKey) {
        saveAction();
        return super.onBackPressed(fromKey);
    }

    @Override
    public void onTopbarLeftPress() {
        onBackPressed(false);
    }


}
